<?php
// Tim'Roster
// Copyright (c) 2011 Nicolas Nallet <aspgic2@gmail.com>
//
// This file is part of Tim'Roster.
//
// Tim'Roster is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Tim'Roster is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

$xml_config = simplexml_load_file('config.xml');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/technical_class/language/language.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/technical_class/authentification/User.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/technical_class/error/Error_Log.php');

function render_error_log_list(){
    $column_array = array();
    $column_array[] = Language::get_instance()->get_label('lb_error');
    $column_array[] = Language::get_instance()->get_label('lb_creation_date');
    $column_array[] = Language::get_instance()->get_label('lb_entity');
    $column_array[] = Language::get_instance()->get_label('lb_entity_type');

    $content = generate_html_sortable_table_header($column_array, null);

    $error_array = Error_Log::get_error_list(date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s'))) . " -1 month")));

    if ((isset($error_array)) && (count($error_array) > 0)){
        foreach($error_array as $err){
            $content .= '<tr>';
            $content .= '<td>'.$err->get_error_type().'</td>';
            $content .= '<td>'.$err->get_creation_date().'</td>';

            $content .= '<td><a href="update_data.php?update=1&';

            if ($err->get_entity_type() == Error_Log::GUILD_ENTITY_TYPE){
                $content .= 'gid='.$err->get_entity_id();
            }
            else {
                $content .= 'cid='.$err->get_entity_id();
            }


            $content .= '" title="'.Language::get_instance()->get_label('lb_relaunch_data_extraction').'" >'.$err->get_entity_name().'</a></td>';

            $content .= '<td>'.$err->get_entity_type().'</td>';
            $content .= '</tr>';
        }
    }

    $content .= generate_html_table_footer();

    return $content;

}


function render_user_list(){
    $column_array = array();
    $column_array[] = Language::get_instance()->get_label('lb_id');
    $column_array[] = Language::get_instance()->get_label('lb_name');
    $column_array[] = Language::get_instance()->get_label('lb_group_label');
    $column_array[] = Language::get_instance()->get_label('lb_email');
    $column_array[] = Language::get_instance()->get_label('lb_creation_date');
    $column_array[] = Language::get_instance()->get_label('lb_last_login');
    $column_array[] = Language::get_instance()->get_label('lb_active');
    $column_array[] = Language::get_instance()->get_label('lb_is_banned');



    $content = generate_html_sortable_table_header($column_array, null);

    $user_array = User::get_user_list(null);

    foreach($user_array as $user){
        $content .= '<tr>';
        $content .= '<td>'.$user->get_id().'</td>';
        $content .= '<td><a href="user.php?id='.$user->get_id().'" >'.ucfirst($user->get_name()).'</a></td>';
        $content .= '<td>'.$user->get_group_label().'</td>';
        $content .= '<td>'.$user->get_email().'</td>';
        $content .= '<td>'.$user->get_creation_date().'</td>';
        $content .= '<td>'.$user->get_last_login().'</td>';
        $content .= '<td>'.$user->get_active().'</td>';
        $content .= '<td>'.$user->get_is_banned().'</td>';
        $content .= '</tr>';
    }

    $content .= generate_html_table_footer();

    return $content;
}


function render_user(User $user){


    $column_array = array();
    $column_array[] = Language::get_instance()->get_label('lb_information');
    $column_array[] = Language::get_instance()->get_label('lb_value');

    $content = generate_html_table_header($column_array, 350, true);
    $content .= generate_html_table_row(Language::get_instance()->get_label('lb_id'), $user->get_id());
    $content .= generate_html_table_row(Language::get_instance()->get_label('lb_name'), ucfirst($user->get_name()));

    if (User::is_user_authorize(User::ADMIN_GROUP_ID)) {
        $content .= generate_html_table_row(Language::get_instance()->get_label('lb_group_label'), render_user_group_combo($user->get_group_id()));
    }
    else {
        $content .= generate_html_table_row(Language::get_instance()->get_label('lb_group_label'), $user->get_group_label());
    }

    $content .= generate_html_table_row(Language::get_instance()->get_label('lb_email'), $user->get_email());
    $content .= generate_html_table_row(Language::get_instance()->get_label('lb_creation_date'), $user->get_creation_date());
    $content .= generate_html_table_row(Language::get_instance()->get_label('lb_last_login'), $user->get_last_login());
    $content .= generate_html_table_row(Language::get_instance()->get_label('lb_active'), $user->get_active());
    $content .= generate_html_table_row(Language::get_instance()->get_label('lb_is_banned'), $user->get_is_banned());
    $content .= generate_html_table_row(Language::get_instance()->get_label('lb_deban_date'), $user->get_deban_date());
    $content .= generate_html_table_row(Language::get_instance()->get_label('lb_login_failed_count'), $user->get_login_failed_count());
    $content .= generate_html_table_row(Language::get_instance()->get_label('lb_last_login_failed_date'), $user->get_last_login_failed_date());

    $content .= generate_html_table_footer();

    return $content;
}

function render_user_group_combo($selected_value){
    $content = '<select name="group_dd" id="group_dd" onchange="changeGroup();">';

    $group_array = User_Group::get_user_group_list();

    foreach ($group_array as $group){
        $content .= '<option id="dd_child" ';

        if ($group->get_id() == $selected_value){ $content .= ' selected="selected" '; }

        $content .= ' value="'.$group->get_id().'" >'.$group->get_label().'</option>';
    }

    $content .= '</select>';
    
    return $content;
}


?>

